package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CheckGroupServiceimpl implements CheckGroupService {
    @Autowired
    CheckGroupDao checkGroupDao;
    @Override
    public void add(Integer[] checkitemIds, CheckGroup checkGroup) {
        checkGroupDao.add(checkGroup);
        System.out.println(checkGroup.getId()+"wwqwewq");
        if (checkGroup.getId() != null && checkitemIds != null && checkitemIds.length > 0){
            for (Integer checkitemId : checkitemIds) {
                System.out.println(checkitemId);
                checkGroupDao.set(checkGroup.getId(),checkitemId);
            }
        }else {
            throw new RuntimeException("中间表维护失败!!");
        }

    }

    @Override
    public PageResult findpage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckGroup>page=checkGroupDao.findAll(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page);
    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }

    @Override
    public List<Integer> findcheckgroup_checkitembyid(Integer id) {

        return checkGroupDao.findcheckgroup_checkitembyid(id);
    }

    @Override
    public void edit(Integer[] checkitemIds, CheckGroup checkGroup) {
        checkGroupDao.edit(checkGroup);
        checkGroupDao.deletegroup_item(checkGroup.getId());
        if (checkGroup.getId()!=null&&checkitemIds!=null&&checkitemIds.length>0){
            for (Integer checkitemId : checkitemIds) {
                checkGroupDao.set(checkGroup.getId(),checkitemId);
            }

        }

    }

    @Override
    public void delete(Integer id) {
        Long count=checkGroupDao.findcount(id);
        if (count==0){
            checkGroupDao.deletegroup_item(id);
            checkGroupDao.delete(id);

        }else {
            throw new RuntimeException("检查组被套餐关联,不能删除");
        }

    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAllGroup();
    }
}
